#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

typedef int BTDataType;

typedef struct BinaryTreeNode
{
	BTDataType data;
	struct BinaryTreeNode* left;
	struct BinaryTreeNode* right;
}BTNode;

BTNode* BuyBTNode(BTDataType x)
{
	BTNode* newnode = (BTNode*)malloc(sizeof(BTNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return NULL;
	}
	newnode->data = x;
	newnode->left = NULL;
	newnode->right = NULL;
	return newnode;
}

BTNode* CreatBinaryTree()
{
	BTNode* node1 = BuyBTNode(1);
	BTNode* node2 = BuyBTNode(2);
	BTNode* node3 = BuyBTNode(4);
	BTNode* node4 = BuyBTNode(3);
	BTNode* node5 = BuyBTNode(5);
	BTNode* node6 = BuyBTNode(6);
	BTNode* node7 = BuyBTNode(9);
	BTNode* node8 = BuyBTNode(9);

	node1->left = node2;
	node1->right = node3;
	node2->left = node4;
	node3->right = node6;
	node3->left = node5;
	node5->left = node7;
	node7->right = node8;
	return node1;
}

void PrevOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("N ");
		return;
	}
	printf("%d ", root->data);
	PrevOrder(root->left);
	PrevOrder(root->right);
}

void InOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("N ");
		return;
	}
	InOrder(root->left);
	printf("%d ", root->data);
	InOrder(root->right);
}

void PostOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("N ");
		return;
	}
	PostOrder(root->left);
	printf("%d ", root->data);
	PostOrder(root->right);
}

int BTSize(BTNode* root)
{
	if (root == NULL)
		return 0;
	return BTSize(root->left) + BTSize(root->right) + 1;
}

int BTLeafSize(BTNode* root)
{
	if (root == NULL)
		return 0;
	if (root->left == NULL && root->right == NULL)
		return 1;
	return BTLeafSize(root->left) + BTLeafSize(root->right);
}

//int BTHeight(BTNode* root)
//{
//	if (root == NULL)
//		return 0;
//	return 1 + (BTHeight(root->left) > BTHeight(root->right) ?
//		BTHeight(root->left) : BTHeight(root->right));
//}

int BTHeight(BTNode* root)
{
	if (root == NULL)
		return 0;
	int leftheight = BTHeight(root->left);
	int rightheight = BTHeight(root->right);
	return leftheight > rightheight ? leftheight + 1 : rightheight + 1;

}

int BTLevelKSize(BTNode* root, int k)
{
	assert(k > 0);
	if (root == NULL)
		return 0;
	if (k == 1)
		return 1;
	return BTLevelKSize(root->left, k-1) + BTLevelKSize(root->right, k-1);
}

int main()
{
	BTNode* root = CreatBinaryTree();
	//InOrder(root);
	//PrevOrder(root);
	//PostOrder(root);
	//printf("%d\n", BTSize(root));
	//printf("%d\n", BTLeafSize(root));
	//printf("%d\n", BTHeight(root));
	printf("%d\n", BTLevelKSize(root, 3));

	return 0;
}